import 'package:casdon/common/utils/string_externtion.dart';
import 'package:casdon/modules/recipe/data/api/recipe_today_api.dart';
import 'package:casdon/modules/recipe/data/model/recipe_placeholder_data.dart';
import 'package:casdon/modules/recipe/ui/widgets/Recipe_card_item.dart';

enum TodayRecipeSortType {
  view, // 按点赞量， 最热，默认
  createTime, // 按 发布时间（id号排序）， 最新
}

class RecipeTodayModel extends RecipeCardData {
  @override
  int get sort => 0;
  final String id;
  final String name;
  final String mainImg;
  final String view;
  final String likes;
  final String collection;
  final bool isControl;
  @override
  final bool isVideo;

  RecipeTodayModel({
    required this.id,
    required this.name,
    required this.mainImg,
    required this.view,
    required this.likes,
    required this.collection,
    required this.isControl,
    required this.isVideo,
  });

  @override
  bool get isSmart => isControl;

  @override
  String get recipeId => id;

  @override
  String? get url => null;

  @override
  String get imgUrl => mainImg.cropParams700;

  @override
  RecipeCardData placeholder() {
    throw UnimplementedError();
  }

  @override
  String get title => name;

  @override
  String get viewCount => view;

  @override
  String get count => "0";

  // 静态方法转换
  static RecipeTodayModel fromApi(RecipeToday response) {
    final rawVideoCover = (response.videoCoverUrls ?? "").trim();
    final rawImg = (response.mainImg ?? response.imageUrls ?? "").trim();
    String resolvedImg;

    if (rawVideoCover.isNotEmpty) {
      resolvedImg = rawVideoCover.split(",").first;
    } else if (rawImg.isNotEmpty) {
      resolvedImg = rawImg.split(",").first;
    } else {
      resolvedImg = recipePlaceHolderImgUrl;
    }

    return RecipeTodayModel(
      id: response.id,
      isVideo: (response.videoCoverUrls ?? "").trim().isNotEmpty,
      name: response.name,
      mainImg: resolvedImg,
      view: response.view ?? "0",
      likes: response.likes ?? "0",
      collection: response.collection ?? "0",
      isControl: (response.isControl ?? 0) == 1 ? true : false,
    );
  }

  // 批量转换的静态方法
  static List<RecipeTodayModel> listFromApi(List<RecipeToday> items) {
    return items.map(fromApi).toList();
  }
}
